package org.zxyj.manage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.zxyj.core.entity.Pagination;
import org.zxyj.core.errorno.SystemErrorNo;
import org.zxyj.core.exception.ServiceException;
import org.zxyj.core.utils.Common;
import org.zxyj.manage.dao.ICourseTemplateDao;
import org.zxyj.manage.entity.CourseTemplate;
import org.zxyj.manage.entity.CourseTemplateArea;
import org.zxyj.manage.entity.CourseTemplateTmxx;
import org.zxyj.manage.service.ICourseTemplateService;
import org.zxyj.system.domain.User;

/**
 * 课程模板设置实现类
    * @ClassName: CourseTemplateServiceImpl
    * @author liuyong
    * @date 2016年7月8日
 */
@Service
public class CourseTemplateServiceImpl implements ICourseTemplateService{
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ICourseTemplateDao courseTemplateDao;
	
	public List<CourseTemplate> listEntities(Map<String, Object> criteria) {
		logger.debug("listing entities refer to criteria, param: [{}]", criteria);

		if (null == criteria) {
			criteria = new HashMap<String, Object>();
		}
		return courseTemplateDao.find(criteria);
	}

	public Pagination listEntities(Map<String, Object> criteria, Pagination pagination) {
		logger.debug("listing entities refer to criteria and pagination, param: criteria[{}], pagination[{}]", criteria, pagination);
		if (null == criteria) {
			criteria = new HashMap<String, Object>();
		}
		criteria.put("pagination", pagination);
		if (null == pagination) {
			pagination = new Pagination();
		}
		pagination.setTotal(courseTemplateDao.count(criteria));
		pagination.setDatas(courseTemplateDao.find(criteria));
		return pagination;
	}
	
	/**
	 * 查询记录-列表页面用
	    * @Title: getListEntities
	    * @param @param criteria
	    * @param @return    参数
	    * @return List<Map<String,Object>>    返回类型
	    * @throws
	 */
	public List<Map<String,Object>> getListEntities(Map<String, Object> criteria){
		logger.debug("listing entities refer to criteria, param: [{}]", criteria);
		if (null == criteria) {
			criteria = new HashMap<String, Object>();
		}
		return courseTemplateDao.getList(criteria);
	}
	
	public Pagination getListEntities(Map<String, Object> criteria, Pagination pagination) {
		logger.debug("listing entities refer to criteria and pagination, param: criteria[{}], pagination[{}]", criteria, pagination);
		if (null == criteria) {
			criteria = new HashMap<String, Object>();
		}
		criteria.put("pagination", pagination);
		if (null == pagination) {
			pagination = new Pagination();
		}
		pagination.setTotal(courseTemplateDao.getListCount(criteria));
		pagination.setDatas(getListEntities(criteria));
		return pagination;
	}
	
	public CourseTemplate findEntity(Map<String, Object> criteria) {
		logger.debug("find CourseTemplate refer to criteria, param: criteria[{}]", criteria);

		if (null == criteria) {
			// 参数错误
			return null;
		}
		return courseTemplateDao.findById(criteria);
	}
	
	public CourseTemplateArea findAreaEntity(Map<String, Object> criteria) {
		logger.debug("find CourseTemplateArea refer to criteria, param: criteria[{}]", criteria);
		
		if (null == criteria) {
			// 参数错误
			return null;
		}
		return courseTemplateDao.getCourseTemplateAreaList(criteria).get(0);
	}

	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdateEntity(CourseTemplate entity,String courseTemplateIdOld) {
		logger.debug("persistenting entity to DB, param: entity[{}]", entity);

		// 参数校验
		if (null == entity) {
			logger.warn("persistenting entity to DB, param entity is null.");
			throw new ServiceException(SystemErrorNo.PARAMETER_IS_NULL);
		}

		User user = (User) Common.findUserSession();
		entity.setCreator(user);
		courseTemplateDao.insert(entity);
		//新文件上传成功后执行删除原文件记录
		if(StringUtils.isNotEmpty(courseTemplateIdOld)){
			Map<String, Object> criteria = new HashMap<String, Object>();
			criteria.put("courseTemplateId", courseTemplateIdOld);
			courseTemplateDao.delete(criteria);
			courseTemplateDao.deleteArea(criteria);
			courseTemplateDao.deleteTmxx(criteria);
		}
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void deleteTemplate(Map<String,Object> criteria){
		//删除文件记录
		courseTemplateDao.delete(criteria);
		courseTemplateDao.deleteArea(criteria);
		courseTemplateDao.deleteTmxx(criteria);
	}
	
	/**
	 * 保存截图信息
	    * @Title: insertArea
	    * @param @param criteria    参数
	    * @return void    返回类型
	    * @throws
	 */
	@Transactional(rollbackFor = Exception.class)
	public void insertArea(Map<String, Object> criteria){
		this.courseTemplateDao.insertArea(criteria);
	}
	
	/**
	 * 更新截图信息
	 * @Title: updateArea
	 * @param @param criteria    参数
	 * @return void    返回类型
	 * @throws
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateArea(Map<String, Object> criteria){
		this.courseTemplateDao.updateArea(criteria);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void deleteAreaEntity(Map<String, Object> criteria) {
		logger.debug("deleting entity from DB, param: criteria[{}]", criteria);

		if (!MapUtils.isEmpty(criteria)) {
			courseTemplateDao.deleteArea(criteria);
		}
	}
	
	/**
	 * 保存小题目信息
	    * @Title: insertMoreTmxx
	    * @param @param tmxxList    参数
	    * @return void    返回类型
	    * @throws
	 */
	@Transactional(rollbackFor = Exception.class)
	public void insertMoreTmxx(List<Map<String,Object>> tmxxList){
		logger.debug("insert insertMoreTmxx from DB, param: criteria[{}]", tmxxList);
		
		this.courseTemplateDao.insertMoreTmxx(tmxxList);
	}
	
	/**
	 * 更新小题信息
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateTmxx(Map<String,Object> map){
		logger.debug("insert updateTmxx from DB, param: criteria[{}]", map);
		
		this.courseTemplateDao.updateTmxx(map);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void deleteTmxxEntity(Map<String, Object> criteria) {
		logger.debug("deleting entity from DB, param: criteria[{}]", criteria);
		
		if (!MapUtils.isEmpty(criteria)) {
			courseTemplateDao.deleteTmxx(criteria);
		}
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void updateAreaPos(Map<String, Object> criteria){
		logger.debug("update entity from DB, param: criteria[{}]", criteria);
		if (!MapUtils.isEmpty(criteria)) {
			courseTemplateDao.updateAreaPos(criteria);
		}
	}
	
	/**
	 * 更新考号采集记录填涂区大小
	    * @Title: updateKhttcjAreaInfo
	    * @param @param criteria    参数
	    * @return void    返回类型
	    * @throws
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateKhttcjAreaInfo(Map<String, Object> criteria){
		logger.debug("update updateKhttcjAreaInfo from DB, param: criteria[{}]", criteria);
		if (!MapUtils.isEmpty(criteria)) {
			courseTemplateDao.updateKhttcjAreaInfo(criteria);
		}
	}

	public List<Map<String,Object>> getExamBatchs(Map<String, Object> criteria) {
		return this.courseTemplateDao.getExamBatchs(criteria);
	}
	public List<Map<String,Object>> getExamGradeByBatch(Map<String, Object> criteria) {
		return this.courseTemplateDao.getExamGradeByBatch(criteria);
	}
	public List<Map<String,Object>> getExamCourseByGrade(Map<String, Object> criteria) {
		return this.courseTemplateDao.getExamCourseByGrade(criteria);
	}

	public List<CourseTemplateArea> getCourseTemplateAreaList(Map<String, Object> criteria){
		return this.courseTemplateDao.getCourseTemplateAreaList(criteria);
	}
	
	public List<CourseTemplateTmxx> getTmxxListEntities(Map<String, Object> criteria){
		return this.courseTemplateDao.getCourseTemplateTmxxList(criteria);
	}
	
	/**
	 * 查询截图区域大题号是否存在
	    * @Title: checkThExistsForArea
	    * @param @param criteria
	    * @param @return    参数
	    * @return boolean    返回类型
	    * @throws
	 */
	public boolean checkThExistsForArea(Map<String, Object> criteria){
		List<CourseTemplateArea> list = this.courseTemplateDao.getCourseTemplateAreaList(criteria);
		if(CollectionUtils.isEmpty(list)){
			return true;
		}
		return false;
	}
	
	/**
	 * 查询题目区域小题号是否存在
	    * @Title: checkThExistsForTmxx
	    * @param @param criteria
	    * @param @return    参数
	    * @return boolean    返回类型
	    * @throws
	 */
	public boolean checkThExistsForTmxx(Map<String, Object> criteria){
		List<CourseTemplateTmxx> list = this.courseTemplateDao.getCourseTemplateTmxxList(criteria);
		if(CollectionUtils.isEmpty(list)){
			return true;
		}
		return false;
	}
	
	/**
	 * 查询单选题起始题号是否存在
	    * @Title: checkQsthExistsForArea
	    * @param @param criteria
	    * @param @return    参数
	    * @return boolean    返回类型
	    * @throws
	 */
	public boolean checkQsthExistsForArea(Map<String, Object> criteria){
		List<CourseTemplateArea> list = this.courseTemplateDao.getCourseTemplateAreaList(criteria);
		if(CollectionUtils.isEmpty(list)){
			return true;
		}
		return false;
	}
	
	/**
	 * 查询模板划定区域信息
	    * @Title: queryCourseTemplateArea
	    * @param @param criteria
	    * @param @return    参数
	    * @return List<Map<String,Object>>    返回类型
	    * @throws
	 */
	public List<Map<String,Object>> queryCourseTemplateArea(Map<String, Object> criteria){
		return this.courseTemplateDao.queryCourseTemplateArea(criteria);
	}

	/**
	 * 获取科目模板集合
	 */
	public Map<Integer, String> getTemplateList(Map<String, Object> criteria) {
		//查询科目模板id
		Map<Integer,String> map = new HashMap<Integer,String>();
		List<CourseTemplate> list = this.listEntities(criteria);
		if(!CollectionUtils.isEmpty(list)){
			for(CourseTemplate t : list){
				map.put(t.getZfmType(), t.getId());
			}
		}
		return map;
	}

	public Map<String, CourseTemplateArea> getTemplateAreaMap(Map<String, Object> criteria) {
		Map<String,CourseTemplateArea> mbMap = new HashMap<String,CourseTemplateArea>();
  		List<CourseTemplateArea> areaList = this.getCourseTemplateAreaList(criteria);
  		for(CourseTemplateArea r : areaList){
  			if("1".equals(r.getLx())){
  				mbMap.put("kh", r);//考号区
  			}else{
  				mbMap.put("qk", r);//缺考区
  			}
  		}
  		return mbMap;
	}

	public Map<String,Map<String,CourseTemplateArea>> getTemplateParams(Map<String, Object> criteria){
		Map<String,Map<String,CourseTemplateArea>> map = new HashMap<String,Map<String,CourseTemplateArea>>();//裁剪参数组合map
		Map<String,CourseTemplateArea> singleMap = new HashMap<String,CourseTemplateArea>();//单选题
        Map<String,CourseTemplateArea> moreMap = new HashMap<String,CourseTemplateArea>();//多选题
        Map<String,CourseTemplateArea> zgMap1 = new HashMap<String,CourseTemplateArea>();//正面主观题
        Map<String,CourseTemplateArea> zgMap2 = new HashMap<String,CourseTemplateArea>();//反面主观题
        List<CourseTemplateArea> list = this.courseTemplateDao.getCourseTemplateAreaList(criteria);
        if(!CollectionUtils.isEmpty(list)){
            for(CourseTemplateArea record : list){
                String rid = record.getId();
                String lx = record.getLx();//区域类型
                //int zfm = record.getCourseTemplate().getZfmType();//正反面
                if(lx.equals("3")){//客观题单选区域
                    singleMap.put(rid, record);
                }else if(lx.equals("4")){//客观题多选区域
                    moreMap.put(rid, record);
                }else if(lx.equals("5")){//主观题区域
                    zgMap1.put(rid, record);
                }
            }
            map.put("singleMap", singleMap);
            map.put("moreMap", moreMap);
            map.put("zgMap1", zgMap1);
            map.put("zgMap2", zgMap2);
        }
		return map;
	}
	
}
